package sctu.edu.java.year2016.team01.OurTeamProject.subroutine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Created by Zero on 2016/6/25.
 */
public class Login {


    public void setUp() throws Exception {

        Register register = new Register();
        Read validate=new Read();
        FunctionList fl=new FunctionList();
        Solution solution=new Solution();

        File file = new File("D:/", "user.txt");
        if (!file.exists()) {
            file.createNewFile();
        }

        JFrame f = new JFrame();
        f.setLayout(null);
        f.setTitle("用户登录");
        f.setBounds(700, 350, 700, 400);
        Container con = f.getContentPane();
        JPanel pan = new JPanel();

        ImageIcon background = new ImageIcon(register.path() + "\\背景.png");
        JLabel label = new JLabel(background);
        label.setBounds(0, 0, 700, 400);
        JPanel imagePanel = (JPanel) f.getContentPane();
        imagePanel.setOpaque(false);
        f.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));


        JLabel accountNumber = new JLabel("账号：");
        accountNumber.setFont(new Font("宋体", Font.BOLD, 21));
        accountNumber.setForeground(Color.white);
        f.getContentPane().add(accountNumber);
        accountNumber.setBounds(100, 120, 80, 30);


        JTextField numberContent = new JTextField(20);
        f.getContentPane().add(numberContent);
        numberContent.setBounds(160, 120, 200, 30);
        numberContent.setForeground(Color.white);
        numberContent.setOpaque(false);


        JLabel password = new JLabel("密码：");
        password.setFont(new Font("宋体", Font.BOLD, 21));
        password.setForeground(Color.white);
        f.getContentPane().add(password);
        password.setBounds(100, 170, 80, 30);


        JTextField passwordContent = new JTextField(20);
        f.getContentPane().add(passwordContent);
        passwordContent.setBounds(160, 170, 200, 30);
        passwordContent.setForeground(Color.white);
        passwordContent.setOpaque(false);


        JButton register2 = new JButton("登陆");
        register2.setFont(new Font("宋体", Font.BOLD, 19));
        register2.setContentAreaFilled(false);
        register2.setForeground(Color.white);
        register2.setBounds(245, 240, 115, 40);
        f.getContentPane().add(register2);



        register2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try {


                    String accounNumber = "guanliyuan";
                    String password = "123456";

                    if (numberContent.getText().equals(accounNumber)
                            && passwordContent.getText().equals(password)) {

                        solution.solution();
                        f.dispose();

                    }


                    for (int i = 0; i < validate.txtName("D:/user.txt").size(); i++) {
                        if (validate.txtName("D:/user.txt").get(i).equals(numberContent.getText() + "*" +
                                passwordContent.getText())) {

                            fl.functionList();
                            f.dispose();


                        } else {
                            JLabel accountNumber = new JLabel("用户名或密码输入错误...");
                            accountNumber.setFont(new Font("宋体", Font.BOLD, 21));
                            accountNumber.setForeground(Color.white);
                            f.getContentPane().add(accountNumber);
                            accountNumber.setBounds(400, 240, 300, 40);
                        }
                    }


                } catch (Exception e1) {
                    e1.printStackTrace();
                }

            }
        });


        f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        con.add(pan);
        f.setVisible(true);
    }
}
